/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.ds2.cafeteria.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mx.itschapala.ds2.cafeteria.modelo.Usuario;

/**
 *
 * @author Santiago
 */
@Stateless
public class UsuarioDao extends AbstractDao<Usuario> implements UsuarioDaoLocal {

    @PersistenceContext(unitName = "cafeteria-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UsuarioDao() {
        super(Usuario.class);
    }

    @Override
    public boolean findByDuplicity(Usuario user) {
        Query q = em.createNamedQuery("Usuario.findByDuplicity");
        q.setParameter("noControl", user.getNoControl());
        return !q.getResultList().isEmpty();
    }

    public Usuario findByNoControl(Usuario user) {
        Query q = em.createNamedQuery("Usuario.findByNoControl");
        q.setParameter("noControl", user.getNoControl());
        if (q.getResultList().isEmpty()) {
            return null;
        } else {
            return (Usuario) q.getResultList().get(0);
        }
    }
}
